package com.pkk.penguin.frame.know.java.driven.condition.condition.simple;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

/**
 * @description: linux的条件
 * @author: peikunkun
 * @create: 2020-07-17 17:44
 **/
@Slf4j
public class LinuxCondition implements Condition {


  /**
   * class信息
   */
  public static final String CLASS_INFO = LinuxCondition.class.getSimpleName();

  /**
   * Determine if the condition matches.
   *
   * @param context the condition context:要使用的上下文环境
   * @param metadata metadata of the {@link AnnotationMetadata class} or {@link MethodMetadata method} being checked:获取当前【所有】注解的注解信息
   * @return {@code true} if the condition matches and the component can be registered, or {@code false} to veto the
   * annotated component's registration
   */
  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    final ClassLoader classLoader = context.getClassLoader();
    log.info("[{}]-使用的类加载器[{}]", CLASS_INFO, classLoader);
    final Environment environment = context.getEnvironment();
    final String osName = environment.getProperty("os.name");
    log.info("[{}]-当前系统为：[{}]", CLASS_INFO, osName);

    //可以获取到所有要注册的bean,对bean的各种操作
    final BeanDefinitionRegistry registry = context.getRegistry();
    if (!StringUtil.isBlank(osName) && osName.toLowerCase().contains("linux")) {
      return true;
    }
    return false;
  }
}
